package com.xbongbong.saas.script.tempenum;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.enums.DefaultValueEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.NumericalLimitsPoJo;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.RefundStatusEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.SetTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.dictionary.AllInBoundEnum;
import com.xbongbong.saas.enums.dictionary.RefundReasonEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 退货退款单初始化枚举
 *
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/16 20:16
 * @since v1.0
 */
@Getter
public enum TempRefundEnum {
    //刷脚本专用枚举
    BASE_INFO(RefundEnum.BASE_INFO),
    REFUND_NO(RefundEnum.REFUND_NO),
    CUSTOMER_ID(RefundEnum.CUSTOMER_ID),
    CONTRACT_ID(RefundEnum.CONTRACT_ID),
    REFUND_NAME(RefundEnum.REFUND_NAME),
    SIGN_USER(RefundEnum.SIGN_USER),
    SIGN_DATE(RefundEnum.SIGN_DATE),
    OWNER_ID(RefundEnum.OWNER_ID),
    COUSERID(RefundEnum.COUSERID),

    RETURN_INFO(RefundEnum.RETURN_INFO),
    PRODUCTS(RefundEnum.PRODUCTS),
    RETURN_AMOUNT(RefundEnum.RETURN_AMOUNT),
    REFUND_REASON(RefundEnum.REFUND_REASON),
    REFUND_STATUS(RefundEnum.REFUND_STATUS),
    MEMO(RefundEnum.MEMO),

    REFUND_INFO(RefundEnum.REFUND_INFO),
    PAYMENT_SHEETS(RefundEnum.PAYMENT_SHEETS),
    REFUND_AMOUNT(RefundEnum.REFUND_AMOUNT),
    INVOICES(RefundEnum.INVOICES),
    PAYMENTS(RefundEnum.PAYMENTS),

    SYSTEM_INFO(RefundEnum.SYSTEM_INFO),
    CREATOR_ID(RefundEnum.CREATOR_ID),
    ADD_TIME(RefundEnum.ADD_TIME),
    UPDATE_TIME(RefundEnum.UPDATE_TIME),

    RED_CONTRACT_ID(RefundEnum.RED_CONTRACT_ID),
    CUSTOMER_ID_LINKED_TEXT(RefundEnum.CUSTOMER_ID_LINKED_TEXT),
    CONTRACT_ID_LINKED_TEXT(RefundEnum.CONTRACT_ID_LINKED_TEXT),
    PAYMENT_SHEETS_LINKED_TEXT(RefundEnum.PAYMENT_SHEETS_LINKED_TEXT),
    INVOICES_LINKED_TEXT(RefundEnum.INVOICES_LINKED_TEXT),
    PAYMENTS_LINKED_TEXT(RefundEnum.PAYMENTS_LINKED_TEXT),
    INSTOCK_ID(RefundEnum.INSTOCK_ID),
    ALL_IN_BOUND(RefundEnum.ALL_IN_BOUND),;

    private RefundEnum anEnum;

    TempRefundEnum(RefundEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempRefundEnum> getAllEnum() {
        List<TempRefundEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }


    /**
     * 初始化编号
     *
     * @param fieldAttrEntity 属性
     */
    public static void initRefundNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.REFUND.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.REFUND.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(REFUND_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
        fieldAttrEntity.setSerialNumber(InitExplainsHelper.setSerialNumber(SerialNumberEnum.REFUND_NO));
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initRefundName(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.REFUND.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.REFUND.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(REFUND_NAME.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
    }

    /**
     * 初始化退货原因
     *
     * @param fieldAttrEntity 属性
     */
    public static void initRefundReason(FieldAttrEntity fieldAttrEntity) {
//        String str = "产品损坏,质量问题,发错产品,其他原因";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(RefundReasonEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(1);
    }

    public static void initCustomerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setLinkedText(CUSTOMER_ID_LINKED_TEXT.anEnum.getAttr());
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initContractId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.CONTRACT.getCode());
        fieldAttrEntity.setLinkedText(CONTRACT_ID_LINKED_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyMode(XbbRefTypeEnum.CONTRACT.getAlias());
        saasParticularAttributePoJo.setRelyType("base");
        saasParticularAttributePoJo.setRelyAttr(CUSTOMER_ID.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);

    }

    public static void initProducts(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.PRODUCT.getCode());
        fieldAttrEntity.setLinkedText(CONTRACT_ID_LINKED_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyMode(XbbRefTypeEnum.CONTRACT.getAlias());
        saasParticularAttributePoJo.setRelyType("relied");
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CONTRACT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    public static void initPaymentSheets(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.PAYMENT_SHEET.getCode());
        fieldAttrEntity.setLinkedText(PAYMENT_SHEETS_LINKED_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyAttr(CONTRACT_ID.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CONTRACT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    public static void initInvoices(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.INVOICE.getCode());
        fieldAttrEntity.setLinkedText(INVOICES_LINKED_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyAttr(CONTRACT_ID.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CONTRACT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    public static void initPayments(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.PAYMENT.getCode());
        fieldAttrEntity.setLinkedText(PAYMENTS_LINKED_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyAttr(CONTRACT_ID.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CONTRACT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    public static void initRefundAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(BasicConstant.ZERO);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setSummaryFlag(1);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias()));
    }

    public static void initReturnAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(1);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setIsForbidden(0);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.单价小计");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setShowEditable(1);
    }

    /**
     * 初始化签订时间
     * @param fieldAttrEntity
     */
    public static void initSignDate(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.TODAY.getAlias());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setSetType(SetTypeEnum.DISABLE_REPEAT.getCode());
    }

    /**
     * 初始化签订人
     * @param fieldAttrEntity
     */
    public static void initSignUser(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedText(SIGN_USER.getAnEnum().getAttr());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.DYNAMIC.getAlias());
        defaultAttrPoJo.setDefaultValue(DefaultValueEnum.LOGIN_USER.getValue());
        defaultAttrPoJo.setDefaultList(new ArrayList<>());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setSetType(SetTypeEnum.DISABLE_REPEAT.getCode());
    }

    public static void initCoUserId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setRequired(0);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增协同人，支持自定义；
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        //可删除权限：默认【超级管理员、销售主管、负责人、负责人主管】可删除协同人，支持自定义；
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }
    public static void initOwnerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setRequired(1);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增负责人，支持自定义；
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setMultiple(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }
    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initAllInBound(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(AllInBoundEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initRefundStatus(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIsDictionary(1);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setEditable(0);
        List<ItemPoJo> itemPoJos = InitExplainsHelper.setCombItemByEnum(RefundStatusEnum.class, fieldAttrEntity.getFieldType());
        List<String> shipStatusCodeList = Arrays.asList(RefundStatusEnum.UNREFUND.getName(), RefundStatusEnum.PARTIAL.getName(), RefundStatusEnum.REFUND.getName(), RefundStatusEnum.CLOSED.getName());
        for (ItemPoJo itemPoJo: itemPoJos) {
            itemPoJo.setNoDel(1);
            itemPoJo.setNoEdit(1);
            if (!shipStatusCodeList.contains(itemPoJo.getText())) {
                itemPoJo.setShowType(ShowTypeEnum.NONE.getCode());
                itemPoJo.setEditHide(1);
            } else {
                if (Objects.equals(itemPoJo.getText(), RefundStatusEnum.UNREFUND.getName())) {
                    itemPoJo.setChecked(true);
                }
            }
        }
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_OPTION_RELY.getAlias(), ForbiddenSettingEnum.DISABLE_SELF_DEFINE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setItemBanDefault(1);
        fieldAttrEntity.setItems(itemPoJos);
    }
}
